﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using System.Configuration;


namespace WebSys.UI.OfficeFlow
{
    public class SuspendSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        DropDownList ddlWname;
        Button btnOK, btnSuspend;
        protected global::Antu.WebControls.CustomGridView gv;
        Label labMsg;
        TextBox txtIId, txtName;
        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                if (base.Page.Request.QueryString.Count > 0)
                {
                    string action = base.Page.Request["action"].ToString();
                    if (action == "qxsq")
                    {
                        string iid = base.Page.Request["iid"].ToString();
                        if (!string.IsNullOrEmpty(iid))
                        {
                            if (_FlowFactoryProvider.NewCancelApplySuspend(iid, UserId))
                            {
                                base.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('取消申请成功')</script>");
                            }
                        }
                    }
                }
                BindDDL();
                BindGridView();                
            }
        }
      
        private void InitControl()
        {
            ddlWname = (DropDownList)FindControl("ddlWname");
            btnOK = (Button)FindControl("btnOK");
            btnSuspend = (Button)FindControl("btnSuspend");
            gv = (Antu.WebControls.CustomGridView)FindControl("gv");
            labMsg = (Label)FindControl("labMsg");
            txtIId = (TextBox)FindControl("txtIId");
            txtName = (TextBox)FindControl("txtName");
            btnOK.Click += new EventHandler(btnOK_Click);
            btnSuspend.Click += new EventHandler(btnSuspend_Click);
            gv.OnLoadData += new EventHandler(gv_OnLoadData);
        }
        protected void gv_OnLoadData(object sender, EventArgs e)
        {
            BindGridView();
        }

        private void BindDDL()
        {
            try
            {
                string ajlx = ConfigurationManager.AppSettings["ZSAnJianType"].ToString(); 
                DataTable dt = _FlowFactoryProvider.GetApplySuspendWame(ajlx);
                ddlWname.DataSource = dt;
                ddlWname.DataTextField = "wname";
                ddlWname.DataValueField = "wname";
                ddlWname.DataBind();
                ListItem item = new ListItem();
                item.Text = dt.Rows.Count == 0 ? "-- 无申请挂起流程 -- " : "-- 申请挂起流程 -- ";
                item.Value = "";
                ddlWname.Items.Insert(0, item);
            }
            catch (Exception e)
            {
                labMsg.Text = e.Message;
            }

        }

        private void BindGridView()
        {
            try
            {
                labMsg.Text = "";
                DataTable dt = _FlowFactoryProvider.GetApplySuspendData(txtIId.Text.Trim(), ddlWname.SelectedValue, txtName.Text.Trim());
                gv.DataSource = dt.DefaultView;
                gv.RecordCount = dt.Rows.Count;
                gv.DataBind();
                labMsg.Text = dt.Rows.Count == 0 ? "无匹配项" : "";
                if (dt.Rows.Count==0)
                {
                    btnSuspend.Visible = false;
                }
            }
            catch (Exception)
            {
                labMsg.Text = "绑定申请挂起列表出错";
            }
        }


        protected void btnOK_Click(object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void btnSuspend_Click(object sender, EventArgs e)
        {
            string id = "";
            foreach (GridViewRow row in gv.Rows)
            {
                CheckBox cb = ((CheckBox)row.Cells[0].FindControl("cb"));
                HiddenField hidid=((HiddenField)row.Cells[0].FindControl("hidid"));
                if (cb.Checked == true)
                {
                    id += "," + hidid.Value;
                }
            }
            id = id.Substring(1);
            string[] strArray = id.Split(',');

            DataTable dt;
            try
            {
                for (int i = 0; i < strArray.Length; i++)
                {
                    dt = _FlowFactoryProvider.GetApplySuspendDataByID(strArray[i].ToString());
                    bool result = _FlowFactoryProvider.NewSuspendWorkFlow(dt.Rows[0]["serial"].ToString(), dt.Rows[0]["handleid"].ToString(), UserId, dt.Rows[0]["memo"].ToString());
                    if (!result)
                        throw new Exception("挂起流程过程出错");
                }
                labMsg.Text = "操作成功！";
                BindGridView();
            }
            catch (Exception eSuspendAA)
            {
                labMsg.Text = "处理申请挂起出错！" + eSuspendAA.Message;
            }

        }


    }
}


